package com.bd.view;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;

import com.bd.components.AppTable;
import com.bd.constants.MessageType;
import com.bd.constants.ResourcesConstants;
import com.bd.controller.AppMainController;
import com.bd.model.CD;
import com.bd.model.Faixa;
import com.bd.model.Label;
import com.bd.util.MessageCreator;
import com.bd.util.SwingUtil;
import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;
import com.toedter.calendar.JDateChooser;

public class CDWindow extends JDialog {
	private JTextField				txtTitulo			= new JTextField();
	private JTextField				txtPreco			= new JTextField();
	private JButton					btnSalvar			= new JButton("Salvar");
	private JButton					btnClose			= new JButton("Close");
	private JPanel					panel				= new JPanel();
	private JDateChooser			dataProducao		= new JDateChooser();
	private JDateChooser			dataCompra			= new JDateChooser();
	private JComboBox<Label>		cbGravadora			= new JComboBox<Label>();
	private String					columns				= "3dlu, default, 3dlu, 75dlu, 20dlu, default, 3dlu,default:grow, 3dlu";
	private String					lines				= "3dlu, default, 3dlu, default, 3dlu, default, 3dlu, 105:grow, 3dlu, 40dlu, 3dlu";
	private CD						cd;
	private final JPanel			panel_1				= new JPanel();
	private final JScrollPane		scrollPane			= new JScrollPane();
	private final AppTable<Faixa>	appTable			= new AppTable<Faixa>();
	private final JPanel			panel_2				= new JPanel();
	private final JButton			btnAdicionarFaixa	= new JButton("Adicionar Faixa");
	private final JButton			btnEditar			= new JButton("Editar");
	private final JButton			btnRemover			= new JButton("Remover");

	public CDWindow() {
		super(AppMainController.getMainFrameInstance(), true);
		setTitle("Cadastro de CD");
		initComponents();
		populateComboLabels();
		setSize(new Dimension(507, 470));
		setLocationRelativeTo(null);
	}

	public CDWindow(CD cd) {
		this();
		this.cd = cd;
		populateFields();
	}

	private void populateFields() {
		if (cd != null) {
			txtTitulo.setText(cd.getDescricao());
			txtPreco.setText(cd.getPreco().toPlainString());
			dataCompra.setDate(cd.getDataCompra());
			dataProducao.setDate(cd.getDataGravacao());
			cbGravadora.setSelectedItem(cd.getFkLabel());
		}
	}

	private void populateComboLabels() {
		List<Label> labels = AppMainController.getInstance().findAllLabels();
		cbGravadora.removeAllItems();

		for (Label label : labels)
			cbGravadora.addItem(label);
	}

	private void initComponents() {
		JLabel lblGravadora = new JLabel("Gravadora:");
		JLabel lblPreo = new JLabel("Pre\u00E7o:");
		JLabel lblDataDeCompra = new JLabel("Data de Compra:");
		JLabel lblTtulo = new JLabel("T\u00EDtulo:");
		JLabel lblDataDeProduo = new JLabel("Data de Produ\u00E7\u00E3o:");

		getContentPane().setLayout(new FormLayout(columns, lines));
		getContentPane().add(lblTtulo, "2, 2, left, default");
		getContentPane().add(txtTitulo, "4, 2, 5, 1, fill, default");
		getContentPane().add(lblGravadora, "2, 4, left, default");
		getContentPane().add(cbGravadora, "4, 4, fill, default");
		getContentPane().add(lblPreo, "6, 4, right, default");
		getContentPane().add(txtPreco, "8, 4, fill, default");
		getContentPane().add(lblDataDeCompra, "2, 6, left, default");
		getContentPane().add(dataCompra, "4, 6, fill, fill");
		getContentPane().add(lblDataDeProduo, "6, 6, left, default");
		getContentPane().add(dataProducao, "8, 6, fill, fill");
		panel_1.setBorder(new TitledBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Faixas", TitledBorder.LEADING,
						TitledBorder.TOP, null, null), "", TitledBorder.LEADING, TitledBorder.TOP, null, null));

		getContentPane().add(panel_1, "2, 8, 7, 1, fill, fill");
		panel_1.setLayout(new FormLayout(new ColumnSpec[] { FormFactory.RELATED_GAP_COLSPEC, ColumnSpec.decode("461px:grow"),
						FormFactory.RELATED_GAP_COLSPEC, }, new RowSpec[] { RowSpec.decode("190px:grow"), FormFactory.RELATED_GAP_ROWSPEC,
						RowSpec.decode("max(27dlu;default)"), }));

		panel_1.add(scrollPane, "2, 1, fill, fill");
		appTable.setModel(new DefaultTableModel(new Object[][] {}, new String[] { "Id.", "T\u00EDtulo", "Tipo Composi\u00E7\u00E3o",
						"Tipo Grava\u00E7\u00E3o", "Faixa No.", "Interprete" }));
		appTable.getColumnModel().getColumn(0).setPreferredWidth(28);
		appTable.getColumnModel().getColumn(1).setPreferredWidth(122);
		appTable.getColumnModel().getColumn(2).setPreferredWidth(99);
		appTable.getColumnModel().getColumn(3).setPreferredWidth(92);

		scrollPane.setViewportView(appTable);

		panel_1.add(panel_2, "2, 3, fill, fill");
		panel_2.setLayout(new FormLayout(new ColumnSpec[] { FormFactory.DEFAULT_COLSPEC, FormFactory.RELATED_GAP_COLSPEC,
						ColumnSpec.decode("default:grow"), FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC, ColumnSpec.decode("default:grow"), FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.DEFAULT_COLSPEC, }, new RowSpec[] { RowSpec.decode("default:grow"), }));

		panel_2.add(btnAdicionarFaixa, "1, 1, default, fill");
		btnAdicionarFaixa.setIcon(ResourcesConstants.ICON_ADD);

		panel_2.add(btnEditar, "5, 1, fill, fill");
		btnEditar.setIcon(ResourcesConstants.ICON_EDIT_16);

		btnRemover.setIcon(ResourcesConstants.ICON_REMOVE_16);

		panel_2.add(btnRemover, "9, 1, fill, fill");
		getContentPane().add(panel, "2, 10, 7, 1, fill, fill");

		columns = "3dlu, 65dlu, 3dlu, default:grow, 3dlu, 65dlu, 3dlu";
		lines = "3dlu, default:grow, 3dlu";
		panel.setLayout(new FormLayout(columns, lines));
		panel.add(btnSalvar, "2, 2, fill, fill");
		panel.add(btnClose, "6, 2, fill, fill");

		btnSalvar.addActionListener(new SaveButtonListener());
		btnSalvar.setIcon(ResourcesConstants.ICON_SAVE);
		btnClose.addActionListener(new SwingUtil.CloseButtonListener(this));
		btnClose.setIcon(ResourcesConstants.ICON_CLOSE);
	}

	class SaveButtonListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			if (cd == null)
				cd = new CD();

			cd.setDataCompra(dataCompra.getDate());
			cd.setDescricao(txtTitulo.getText());
			cd.setPreco(new BigDecimal(txtPreco.getText()));
			cd.setFkLabel(cbGravadora.getItemAt(cbGravadora.getSelectedIndex()));
			cd.setDataGravacao(dataProducao.getDate());

			AppMainController.getInstance().saveCD(cd);
			MessageCreator msg = MessageCreator.getMessageInstance("Sucesso!", MessageType.SUCCESS);
			msg.addMessage("Salvo com sucesso!").displayMessage();
		}
	}
}
